//
//  HLVisitorView.swift
//  MyWeiBo
//
//  Created by fannie on 16/3/20.
//  Copyright © 2016年 fannie. All rights reserved.
//

import UIKit

protocol HLVisitorViewDelegate :NSObjectProtocol {
    func visitorViewRegistBtnAction()
    func visitorViewLoginBtnAction()
}

class HLVisitorView: UIView {

    weak var delegate:HLVisitorViewDelegate?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        prepare()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //MARK: -准备加载视图
    func prepare() {
        backgroundColor=UIColor(white: 237 / 255, alpha: 1)
        
        addSubview(wheelImageView)
        addSubview(coverImageView)
        addSubview(homeImageView)
        addSubview(messageLabel)
        addSubview(registerBtn)
        addSubview(loginBtn)
        
        wheelImageView.translatesAutoresizingMaskIntoConstraints=false
        coverImageView.translatesAutoresizingMaskIntoConstraints=false
        homeImageView.translatesAutoresizingMaskIntoConstraints=false
        messageLabel.translatesAutoresizingMaskIntoConstraints=false
        registerBtn.translatesAutoresizingMaskIntoConstraints=false
        loginBtn.translatesAutoresizingMaskIntoConstraints=false
        //wheelImageView约束
        addConstraint(NSLayoutConstraint(item: wheelImageView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: wheelImageView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -40))
        //coverImageView约束
        addConstraint(NSLayoutConstraint(item: coverImageView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: coverImageView, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: coverImageView, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: coverImageView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: registerBtn, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        //homeImageView约束
        addConstraint(NSLayoutConstraint(item: homeImageView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: wheelImageView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: homeImageView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: wheelImageView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        //messageLabel约束
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: wheelImageView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: wheelImageView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 240))
        //registerBtn约束
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        //loginBtn约束
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: registerBtn, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: registerBtn, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 0))
    }
    
    func setupVistorInfo(imageName:String, context:String) {
        homeImageView.hidden=true
        wheelImageView.image=UIImage(named: imageName)
        messageLabel.text=context
        sendSubviewToBack(coverImageView)
    }
    
    func startRorationAnimation() {
        
        let animation=CABasicAnimation(keyPath: "transform.rotation")
        animation.toValue=2*M_PI
        animation.duration=20
        animation.repeatCount=MAXFLOAT
        animation.removedOnCompletion=false
        wheelImageView.layer.addAnimation(animation, forKey: "homeRotation")
    }
   //MARK: -懒加载
    private lazy var wheelImageView:UIImageView = {
        let image=UIImage(named: "visitordiscover_feed_image_smallicon")
        let imageView=UIImageView(image: image)
        return imageView
        }()
    
    private lazy var coverImageView:UIImageView = {
        let image=UIImage(named: "visitordiscover_feed_mask_smallicon")
        let imageView=UIImageView(image: image)
        return imageView
        }()
    
    private lazy var homeImageView:UIImageView = {
        let image=UIImage(named: "visitordiscover_feed_image_house")
        let imageView=UIImageView(image: image)
        return imageView
        }()
    
    private lazy var messageLabel:UILabel = {
        let label=UILabel()
        label.text="关注一些人，来看看有什么惊喜"
        label.textAlignment=NSTextAlignment.Center
        label.textColor=UIColor.darkGrayColor()
        label.numberOfLines=0
        label.font=UIFont.systemFontOfSize(17)
        return label
        }()
    
    private lazy var registerBtn:UIButton = {
        let btn=UIButton()
        btn.setTitle("注册", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.addTarget(self, action: "registBtnClick", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
        }()
    
    private lazy var loginBtn:UIButton = {
        let btn=UIButton()
        btn.setTitle("登录", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.addTarget(self, action: "loginBtnClick", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
        }()
    
    //MARK: -按钮点击事件
    func registBtnClick() {
        delegate?.visitorViewRegistBtnAction()
    }
    
    func loginBtnClick() {
        delegate?.visitorViewLoginBtnAction()
    }
}
